﻿using System;
using System.Collections.Generic;
using MDFGame;
namespace Model
{
    /// <summary>
    /// 游戏根功能存储
    /// </summary>
    public static class GameRoot
    {
        // 组件池
        private static ObjectPool objectPool;
        public static ObjectPool ObjectPool
        {
            get
            {
                return objectPool ?? (objectPool = new ObjectPool());
            }
        }


        //全局组件表
        public static Dictionary<long, Component> allComponents = new Dictionary<long, Component>();
        //全局实体表
        public static Dictionary<long, Entity> allEntities = new Dictionary<long, Entity>();
        //全局系统表
        public static Dictionary<Type,System> allSystems = new Dictionary<Type, System>();

        // 调度系统
        private static DispatcherSystem dispatcherSystem;
        public static DispatcherSystem DispatcherSystem
        {
            get
            {
                if (dispatcherSystem == null)
                {
                    dispatcherSystem = new DispatcherSystem();
                    dispatcherSystem.Create();
                    return dispatcherSystem;
                }
                return dispatcherSystem;
            }
        }

        // 全局组件挂载的实体,所有全局单例组件均挂载于此
        private static Scene scene;
        public static Scene Scene
        {
            get
            {
                if (scene != null)
                    return scene;
                scene = EntityFactory.Create<Scene>();
                return scene;
            }
        }

        //用于直接获取指定的某个系统
        public static T GetSystem<T>()
            where T:System
        {
            Type type = typeof(T);
            if (allSystems.ContainsKey(type))
            {
                return allSystems[type] as T;
            }
            return null;
        }


        public static void Close()
        {
            scene.Dispose();
            dispatcherSystem = null;
            scene = null;
            objectPool = null;
        }
    }
}
